﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace Miao.Stand
{

    public static class StandAbilityMaker
    {
        #region Create
        public static StandAbility CreateStandAbility(string defName, StandPawn standPawn)
        {
            return CreateStandAbility(DefDatabase<StandAbilityDef>.GetNamed(defName), standPawn);
        }
        public static StandAbility CreateStandAbility(StandAbilityDef def, StandPawn standPawn)
        {
            StandAbility standAbility = (StandAbility)Activator.CreateInstance(def.standAbilityClass, standPawn);
            standAbility.def = def;
            standAbility.Id = Find.UniqueIDsManager.GetNextAbilityID();
            standAbility.Init();
            return standAbility;
        }
        #endregion
    }

    public class StandAbility : IExposable, ILoadReferenceable
    {
        public int Id = -1;
        public string GetUniqueLoadID()
        {
            return "StandAbility_" + Id;
        }
        /// <summary>
        /// StandAbilityMake
        /// </summary>
        /// <param name="def"></param>
        /// <returns></returns>

        public StandAbilityDef def;
        public StandPawn stand;


        public StandAbility(StandPawn stand)
        {
            this.stand = stand;
        }
        /// <summary>
        /// 初始化，像准备初始化按钮
        /// </summary>
        virtual public void Init()
        {

        }
        /// <summary>
        /// 当前剩余冷却Tick
        /// </summary>
        private int cooldownTicks;
        /// <summary>
        /// 当前剩余冷却Tick
        /// </summary>
        public int CooldownTicksRemaining => cooldownTicks;
        /// <summary>
        /// 总冷却时间Tick
        /// </summary>
        public int CooldownTicksTotal=> def.cooldown;

        /// <summary>
		/// 是否在冷却当中
        /// 原版的冷却是，冷却完毕后直接施法。
		/// </summary>
		public bool HasCooldown
        {
            get
            {
                return cooldownTicks > 0;
            }

        }



        virtual public void Tick()
        {
            if (cooldownTicks > 0)
            {
                cooldownTicks--;
            }
        }
        /// <summary>
        /// 开始执行并消耗能量
        /// </summary>
        public void StartCast()
        {
            cooldownTicks = CooldownTicksTotal;
            stand.standPower.PowerConsume(this.def.cost);
        }

        virtual public Gizmo DrawGizmo() { return null; }

        public virtual void ExposeData()
        {
            Scribe_Defs.Look(ref def, "standAbilityDef");
            if (def != null)
            {
                Scribe_Values.Look(ref Id, "Id", -1);
                if (Scribe.mode == LoadSaveMode.LoadingVars && Id == -1)//载入字段
                {
                    Id = Find.UniqueIDsManager.GetNextAbilityID();
                }
                Scribe_Values.Look(ref cooldownTicks, "cooldownTicks", 0);
                Init();
            }
        }
        public bool PowerCostEnough(CommandStandCooldown commandStand)
        {
            return PowerCostEnough(commandStand, this.def.minCost);
        }
        /// <summary>
        /// 能量是否足够
        /// </summary>
        /// <param name="commandStand"></param>
        /// <param name="cost"></param>
        /// <returns></returns>
        public bool PowerCostEnough(CommandStandCooldown commandStand,float cost)
        {
            if (stand.standPower.PowerConsumePeek(cost))
            {
                return true;
            }
            commandStand.disabledReason = $"替身能量最小需达到{cost}".Translate();
            return false;
        }
        /// <summary>
        /// 是否冷却完成
        /// </summary>
        /// <param name="commandStand"></param>
        /// <returns></returns>
        public bool CooldownComplete(CommandStandCooldown commandStand)
        {
            if(HasCooldown)
            {
                commandStand.disabledReason = "冷却中".Translate();
                return false;
            }
            return true;
        }
        /// <summary>
        /// 是否是背后跟随模式
        /// </summary>
        /// <param name="commandStand"></param>
        /// <returns></returns>
        public bool GhostFollow(CommandStandCooldown commandStand)
        {
            if (stand.ghostFollow)
            {
                return true;
            }
            commandStand.disabledReason = "必须处于背后灵模式".Translate();
            return false;
        }

        public void SetDefField(CommandStandCooldown commandStand)
        {
            commandStand.defaultLabel = def.label;
            commandStand.defaultDesc = def.description;
            commandStand.icon = Stand.ModSingleton.LoadIcon(def.iconPath);
            commandStand.activateSound = SoundDefOf.Tick_Tiny;
        }

    }
}
